<?php

list($usec, $sec) = explode(" ", microtime());
$begin_generating = sprintf("%.0f", ((double)$usec + (double)$sec)*1000);

function ob_handler($buffer)
{
    $bufferSize = strlen($buffer);
    inc_class("std");
    global $settings, $begin_generating;
    header("X-Generating-Time: " . (std::time()-$begin_generating) . "ms", true);
    $AcceptEncoding = array ();
    foreach ( explode(",", $_SERVER["HTTP_ACCEPT_ENCODING"]) as $value ) {
        $AcceptEncoding[] = strtolower(trim($value));
    }
    if (!empty($settings["COMPRESS_RESPONSE"])) {
        switch (true) {
            case in_array("deflate", $AcceptEncoding):
                $begin_compressing = std::time();
                header("Content-Encoding: deflate", true);
                header("Vary: Accept-Encoding", true);
                $buffer = gzdeflate($buffer, 9);
                header("X-Compress: " . sprintf("%.1f", 100*strlen($buffer)/$bufferSize) . "%", true);
                header("X-Compressing-Time: " . (std::time()-$begin_compressing) . "ms", true);
                break;
            case in_array("gzip", $AcceptEncoding):
                $begin_compressing = std::time();
                header("Content-Encoding: gzip", true);
                header("Vary: Accept-Encoding", true);
                $buffer = gzencode($buffer, 9, FORCE_GZIP);
                header("X-Compressed: " . sprintf("%.1f", 100*strlen($buffer)/$bufferSize) . "%", true);
                header("X-Compressing-Time: " . (std::time()-$begin_compressing) . "ms", true);
                break;
        }
    }
    return $buffer;
}

ob_start("ob_handler");

// System definisions
define("PATH_SLASH",    "\\");
define("PS",            PATH_SLASH);
define("SYSTEM_DIR",    "system");
define("SITE_ROOT",     realpath(dirname(__FILE__)).PS);
define("SYSTEM_ROOT",   SITE_ROOT.SYSTEM_DIR.PS);

require_once SYSTEM_ROOT."ini.inc.php5";
inc_class("sys");
inc_class("component");
inc_class("pcre");
inc_class("fs");

$strScript = $_GET["__sys:path"];
$script = fs::str2path($strScript);

if (fs::path2str($script)=="/ajax") {
    filedump('<?xml version="1.0" encoding="Windows-1251" ?><result>1</result>');
} elseif ($script["extension"]=="php" || $script["extension"]=="php5") {
    $fileName = SITE_ROOT.substr($strScript, 1);
    if (fs::file_exists($fileName)) {
        session_start();
        include_once $fileName;
    } else {
        die("404");
    }
} elseif (strpos(fs::path2str($script), "/rfc/")===0) {
    $fileName = SITE_ROOT."rfc.php";
    if (fs::file_exists($fileName)) {
        session_start();
        include_once $fileName;
    } else {
        die("404");
    }
} elseif (strpos(fs::path2str($script), "/tipograph/")===0) {
    $fileName = SITE_ROOT."tipograph.php";
    if (fs::file_exists($fileName)) {
        session_start();
        include_once $fileName;
    } else {
        die("404");
    }
} elseif (fs::file_exists(SITE_ROOT.substr($strScript, 1))/* && isset($_GET["id"])*/) {
    /*header("ETag: \"11c415a-8206-243aea40\"");
    header("Expires: 23 Feb 2008 09:40:57 GMT");*/

    //header("Cache-Control: cache");
    //header("Location: {$strScript}");
    switch ($script["extension"]) {
        case "css":
            $mime = "text/css";
            break;
        case "xml":
            $mime = "text/xml";
            break;
        case "js":
            $mime = "text/javascript";
            break;
        default:
            $mime = fs::mime(SITE_ROOT.substr($strScript, 1));
    }
    /*header("Cache-Control: cache");*/
    header("Content-Type: ".$mime);
    switch ($mime) {
        case "image/x-png":
        case "image/jpeg":
            $settings["COMPRESS_RESPONSE"] = false;
            break;
    }
    readfile(SITE_ROOT.substr($strScript, 1));
} else {
    die("404");
}

/*

$cubic = new cubic();

$page = new component();


//filedump($page->xml->GetDomXml("icq2")->saveXML());
$ret = $page->xml->Transform("icq2", "icq2html");
die(pcre::replace("^".pcre::escape("<?").".*?".pcre::escape("?>"), "", $ret->saveXML()));

if ($page->xml->Validate("grammar", "grammar")) {
    $ret = $page->xml->Transform("grammar", "grammar2xhtml");
    die(pcre::replace("^".pcre::escape("<?").".*?".pcre::escape("?>"), "", $ret->saveXML()));
} else {
	die("Invalid XML file!");
}

if ($ret = $page->xml->Validate("concept-files", "er-model")) {
	filedump($ret->saveXML());
} else {
	die("Invalid XML file!");
}

if ($page->xml->ValidateBySchema("validate-result", "validate-result")) {
	filedump($page->xml->GetDomXml("validate-result")->saveXML());
} else {
	die("Invalid XML file!");
}

if ($page->xml->ValidateBySchema("result", "result-bool")) {
	filedump($page->xml->GetDomXml("result")->saveXML());
} else {
	die("Invalid XML file!");
}

if ($page->xml->ValidateBySchema("validate/er-model", "validate-proccess")) {
	filedump($page->xml->GetDomXml("validate/er-model")->saveXML());
} else {
	die("Invalid XML file!");
}

*/

?>